
package com.v1.video.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iss.view.common.ToastAlone;
import com.v1.video.R;
import com.v1.video.domain.ResultInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.exception.ServicesException;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;
import com.v1.video.util.TextNumLimitWatcher;
import com.v1.video.util.Utils;
import com.v1.video.widgets.MyDialog;

/**
 * 意见反馈的类
 * @author Administrator
 *
 */
public class AboutIdeaActivity extends BaseActivity {

    private final String TAG = "AboutIdeaActivity";

    private AboutIdeaActivity instance;
//    setText(Html.fromHtml("<u>"+"hahaha"+"</u>"));
    /**运营商下拉框*/
//    private Spinner spinnerMobile;
//    private String[] strArrMobile;
//    private ArrayList<String> listMobile = new ArrayList<String>();
//    private ArrayAdapter<String> adapterMobile;
    private RelativeLayout rlMobileSel;
    private TextView tvMobileSel;
    private TextView tvContact;    
    private MyDialog dialogCommon;
    private int mobileSel = 3;
    private String mobileStrs[] = new String[]{"中国移动", "中国联通", "中国电信", "其他运营商"};
    
    /**返回按钮*/
    private ImageView btnReturn;
//    /**发送按钮*/
//    private ImageView btnSend;          
    
    EditText editTxt_idea = null;
    Button btnSend = null;
    
    private TextView txtTitle = null;
//ⓒ
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_idea);
        instance = this;
        initView();
        initData();
        setListener();
    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
//        spinnerMobile = (Spinner)findViewById(R.id.about_idea_mobile_sel);
        rlMobileSel = (RelativeLayout)findViewById(R.id.rl_about_idea_mobile_sel);
        tvMobileSel = (TextView)findViewById(R.id.tv_about_idea_mobile_sel);
        tvContact = (TextView)findViewById(R.id.about_idea_contact_input);
        
        btnReturn = (ImageView)findViewById(R.id.iv_result);
//        btnSend = (ImageView)findViewById(R.id.head_idea_btn_send);
        btnSend = (Button)findViewById(R.id.person_sign_btn_save);   
        textRemainNum = (TextView)findViewById(R.id.about_idea_text_number);
        editTxt_idea = (EditText)findViewById(R.id.about_idea_edit_input);
        
        txtTitle = (TextView)findViewById(R.id.tv_title);
    }

    @Override
    protected void initData() {
        // TODO Auto-generated method stub
//        updataSpinnerProvince();
//        ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(AboutIdeaActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    	txtTitle.setText(R.string.about_label_idea_feedback);
        editTxt_idea.addTextChangedListener(new TextNumLimitWatcher((TextView) findViewById(R.id.about_idea_text_number), editTxt_idea, this));
        editTxt_idea.setDrawingCacheEnabled(true);
//        handler.sendEmptyMessage(1);
//        SoftKeyBoardRelativeLayout totalView = (SoftKeyBoardRelativeLayout)findViewById(R.id.about_idea_back);
//        totalView.setOnkeyBoardListener(new SoftKeyBoardRelativeLayout.OnKeyBoardListener() {
//            
//            @Override
//            public void onShowKeyBoard(int height) {
//                if(height<=0) return;
//                
//            }
//            
//            @Override
//            public void onHiddenKeyBoard() {
////                handler.sendEmptyMessage(1);
//            }
//        });
        initEdit();
        mobileSel = 3;
        tvMobileSel.setText(mobileStrs[mobileSel]);
    }
    
//    private void updataSpinnerProvince() {
//        strArrMobile = null;
//        listMobile.clear();
//        strArrMobile = getResources().getStringArray(R.array.about_mobile_arr);
//        for(int i = 0; i < strArrMobile.length;i++){
//            listMobile.add(strArrMobile[i]);
//        }
//        Logger.i(TAG, "strArrMobile.length="+strArrMobile.length);
//        for(int i = 0; i < listMobile.size();i++){
//            Logger.i(TAG, "listMobile["+i+"]="+listMobile.get(i));
//        }
//        adapterMobile = new ArrayAdapter<String>(AboutIdeaActivity.this, android.R.layout.simple_spinner_item, listMobile);       
//        adapterMobile.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        spinnerMobile.setAdapter(adapterMobile);
//    }

    @Override
    protected void setListener() {
        // TODO Auto-generated method stub
    	findViewById(R.id.lay_result).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
       
        btnSend.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                if(!Helper.checkConnection(AboutIdeaActivity.this)){
                    ToastAlone.showToast(AboutIdeaActivity.this, getResources().getString(R.string.net_no), 1);
                    return;
                }
                Logger.i(TAG, "发送按钮执行了");
                String opinion = "";
                String contact = "";
                String operator = "";                
                opinion = editTxt_idea.getText().toString();
                
                contact = tvContact.getText().toString();
                operator = tvMobileSel.getText().toString();
                if(opinion.equals("") || opinion.length() <= 0){
                    ToastAlone.showToast(AboutIdeaActivity.this, getResources().getString(R.string.about_idea_no_con), 1);
                }else{
                    new SendDataAsync(opinion, contact, operator).execute();
                }
            }
        });
        rlMobileSel.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mobileSelectShow();
//                aboutIdeaResult();
            }
        });
//        spinnerProvinceOnClick();
    }
    
//    private void spinnerProvinceOnClick() {
//        spinnerMobile.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
//
//            @Override
//            public void onItemSelected(AdapterView<?> arg0, View arg1,
//                    int pos, long arg3) {
//                // TODO Auto-generated method stub
//                                
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> arg0) {
//                // TODO Auto-generated method stub
//                
//            }
//            
//        });
//    }
    
    /** 意见反馈返回提醒*/
    private void aboutIdeaResult() {
        if(dialogCommon != null){
            dialogCommon = null;
        }
        dialogCommon = new MyDialog(this, R.style.dialog_custom);
        RelativeLayout linearlayout;
        android.view.WindowManager.LayoutParams layoutparams;
        linearlayout = (RelativeLayout)((LayoutInflater)this.getSystemService("layout_inflater")).inflate(R.layout.dialog_aboutidea_result, null);
        linearlayout.setMinimumWidth(10000);
        
//        layoutparams = dialogCommon.getWindow().getAttributes();
//        layoutparams.x = 0;
//        layoutparams.y = -1000;
//        layoutparams.gravity = 80;
//        dialogCommon.onWindowAttributesChanged(layoutparams);
        
        dialogCommon.setCanceledOnTouchOutside(true);
        dialogCommon.setContentView(linearlayout);
        dialogCommon.setCancelable(false);
        dialogCommon.setCanceledOnTouchOutside(false);
        //
//        dialogCommon.setContentView(R.layout.dialog_person_mobile);
//        dialogCommon.setCancelable(false);
//        dialogCommon.setCanceledOnTouchOutside(false);
       
        RelativeLayout layoutSure = (RelativeLayout) dialogCommon.findViewById(R.id.aboutidea_btn_back);
       
        layoutSure.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shortDelay();
                dialogCommon.dismiss();
                finish();
//                txtMobileSel.setText(R.string.mobile_select_dx);
            }
        });
        
        dialogCommon.show();
    }
    
    /** 选择运营商 */
    private void mobileSelectShow() {
        if(dialogCommon != null){
            dialogCommon = null;
        }
        dialogCommon = new MyDialog(this, R.style.dialog_custom);
        
        //
        
        LinearLayout linearlayout;
		android.view.WindowManager.LayoutParams layoutparams;
		linearlayout = (LinearLayout)((LayoutInflater)this.getSystemService("layout_inflater")).inflate(R.layout.dialog_person_mobile, null);
		linearlayout.setMinimumWidth(10000);
		
		layoutparams = dialogCommon.getWindow().getAttributes();
		layoutparams.x = 0;
		layoutparams.y = -1000;
		layoutparams.gravity = 80;
		dialogCommon.onWindowAttributesChanged(layoutparams);
		
		dialogCommon.setCanceledOnTouchOutside(true);
		dialogCommon.setContentView(linearlayout);
        
        //
//        dialogCommon.setContentView(R.layout.dialog_person_mobile);
//        dialogCommon.setCancelable(false);
//        dialogCommon.setCanceledOnTouchOutside(false);
        ImageView btnDX = (ImageView) dialogCommon.findViewById(R.id.dialog_mobile_dx_btn);
        ImageView btnLT = (ImageView) dialogCommon.findViewById(R.id.dialog_mobile_lt_btn);
        ImageView btnOther = (ImageView) dialogCommon.findViewById(R.id.dialog_mobile_other_btn);
        ImageView btnMobile = (ImageView) dialogCommon.findViewById(R.id.dialog_mobile_btn);
       
        if (mobileSel == 0) {
            btnMobile.setImageResource(R.drawable.btn_select_on_option);
            btnLT.setImageResource(R.drawable.btn_select_down_option);
            btnOther.setImageResource(R.drawable.btn_select_down_option);
            btnDX.setImageResource(R.drawable.btn_select_down_option);
        } else if (mobileSel == 1) {
            btnDX.setImageResource(R.drawable.btn_select_down_option);
            btnLT.setImageResource(R.drawable.btn_select_on_option);
            btnOther.setImageResource(R.drawable.btn_select_down_option);
            btnMobile.setImageResource(R.drawable.btn_select_down_option);           
        } else if (mobileSel == 2) {
            btnDX.setImageResource(R.drawable.btn_select_on_option);
            btnLT.setImageResource(R.drawable.btn_select_down_option);
            btnOther.setImageResource(R.drawable.btn_select_down_option);
            btnMobile.setImageResource(R.drawable.btn_select_down_option);   
        }else if (mobileSel == 3) {
            btnDX.setImageResource(R.drawable.btn_select_down_option);
            btnLT.setImageResource(R.drawable.btn_select_down_option);
            btnOther.setImageResource(R.drawable.btn_select_on_option);
            btnMobile.setImageResource(R.drawable.btn_select_down_option);  
        }
        dialogCommon.findViewById(R.id.dialog_mobile_dx).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	shortDelay();
                dialogCommon.dismiss();
//                txtMobileSel.setText(R.string.mobile_select_dx);
                mobileSel = 2;
                tvMobileSel.setText(mobileStrs[mobileSel]);
            }
        });
        dialogCommon.findViewById(R.id.dialog_mobile_lt).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	shortDelay();
                dialogCommon.dismiss();
//                txtMobileSel.setText(R.string.mobile_select_lt);
                mobileSel = 1;
                tvMobileSel.setText(mobileStrs[mobileSel]);
            }
        });
        dialogCommon.findViewById(R.id.dialog_mobile_other).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	shortDelay();
                dialogCommon.dismiss();
//                txtMobileSel.setText(R.string.mobile_select_other);
                mobileSel = 3;
                tvMobileSel.setText(mobileStrs[mobileSel]);
            }
        });
        dialogCommon.findViewById(R.id.dialog_mobile_mobile).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	shortDelay();
                dialogCommon.dismiss();
//                txtMobileSel.setText(R.string.mobile_select_mobile);
                mobileSel = 0;
                tvMobileSel.setText(mobileStrs[mobileSel]);
            }
        });
        dialogCommon.show();
    }
    
    Handler handler = new Handler(){

        @Override
        public void dispatchMessage(Message msg) {
            // TODO Auto-generated method stub
            switch(msg.what){
                case 1:
                    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(AboutIdeaActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    break;
            }
        }
        
    };

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }
    public int maxInput = 300;
    
    //dialog
    
    private void shortDelay(){
    	
    	try {
    		Thread.sleep(200);
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	
    }
    
    //字数限制相关
    
    private TextView textRemainNum;
    
    private void initEdit(){
    	
    	InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter.LengthFilter(300) {
            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                    Spanned dest, int dstart, int dend) {
            	int strLength = editTxt_idea.getText().toString().length();
            	textRemainNum.setText("" + (300 - strLength <=0?0:300 - strLength));
                return super.filter(source, start, end, dest, dstart, dend);
            }
        };
        editTxt_idea.setFilters(filters);
    	
    }
    
    /**
     * 意见反馈提交
     */
    private class SendDataAsync extends AsyncTask<Void, Void, Boolean> {
        
        private String errorMsg = "";
        private String opinion;
        private String contact;
        private String operator;
        private ProgressDialog pd;
        ResultInfo resultInfo = null;
        /**
         * 
         * @param userId            用户id
         * @param type       关注，评论，系统
         * @param typeld            (0关，1开)
         */
        public SendDataAsync(String opinion,String contact,String operator) {                        
            this.opinion = opinion;
            this.contact = contact;
            this.operator = operator;
        }
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = Utils.getProgressDialog(AboutIdeaActivity.this,getResources().getString(R.string.get_data), this);
            pd.show();
        }
        
        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                NetEngine engine = new NetEngine();
                resultInfo = engine.sendAboutIdea(opinion, contact, operator);
            } catch (ServicesException e) {
                e.printStackTrace();
                errorMsg = e.getMessage();
            }
            return false;
            
        }
        
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if(pd != null && pd.isShowing()){
                pd.cancel();
            }
            if (errorMsg.equals("")) {
                if(resultInfo.getCode().equals("0")){
//                    ToastAlone.showToast(AboutIdeaActivity.this, resultInfo.getMessage(), 1);
                    aboutIdeaResult();
                }else{
                    ToastAlone.showToast(AboutIdeaActivity.this, resultInfo.getMessage(), 1);
                }
            } else {
                ToastAlone.showToast(AboutIdeaActivity.this, errorMsg, 1);
            }
        }
    }
}
